/**
 * ajax pool for mobile vip qq application
 * @author Levin
 * @version 1.0
 */
JF.M("ajaxPool",(function($){

	var p={},pub={};

	pub.recommend={
		isInit:false,
		url:'http://vivasky.com/labs/mapp/tuan/js/testData.js',
		tpl_item:('<li data-pidx="%pidx%">'+
					'<a href="%url%" data-title="团购简介">'+
						'<img src="%logo%" />'+
						'<div class="info">'+
							'<h2>%desc% <i class="ico_h"></i> <i class="ico_n"></i></h2>'+
							'<p>%addr%</p>'+
							'<p>'+
								'<span class="c2">&yen;%price%</span>'+
								'<span class="c1">%discount%折</span>'+
								'<span class="c1">%numBuy%人购买</span>'+
							'</p>'+
						'</div>'+
					'</a>'+
				'</li>'),
		getPageIdx:function($ul,isNext,future){
			var p=isNext?$ul.find('li:last').data('pidx'):$ul.find('li:eq(0)').data('pidx');
			p=p||0;
			if(!future) return p;
			p=parseInt(p);
			p=isNext?(p+1):(p-1);
			p=p<1?1:p;

			return p;
		},
		load1:function($target,cbk,isNext){
			var me=this,
				$ul=$target.find("ul:eq(0)"),
				p=me.getPageIdx($ul,isNext,true),
				p0=me.getPageIdx($ul,isNext,false),
				resetPullRefresh=function(){
					//参考iscroll的848行，直接调用refresh方法来隐藏loading...
					JF.iscroll.Get($target[0].id).val.refresh();
				};

			//已经是第一页
			if(p===p0){
				if( cbk ) {
					cbk($target,null,true);
					resetPullRefresh();
				};
				return;
			};

			//显示loading
			//$ul.css("visibility","hidden");
			$("#J_pop_mask").show();
			$.mobile.showPageLoadingMsg();

			$.ajax({
				url:this.url,
				data:{
					t:(new Date()).getTime(),
					p:p
				},
				dataType:'script',
				success:function(){
					var d=null,
						fix=null;
					if(d=window["MovieData"]){
						if(d.length==0) {
							if( cbk ) {
								cbk($target,d,false);
								$("#J_pop_mask").hide();
								//$ul.css("visibility","visible");
								$.mobile.hidePageLoadingMsg();
							};
							return;
						}

						//生成dom
						var _html="";
						for(var i=d.length-1;i>=0;i--){
							//TEST:
							d[i].pidx=p+'';
							_html+=JF.EvalTpl(me.tpl_item,d[i]);
						};

						//插入dom
						$ul.html(_html);

						if( cbk ) {
							cbk($target,d,false); 
						};

						//更新iscroll
						me.scrollShow($target,_html,{
							$ul:$ul
						});
					};
						
				},
				error:function(){
					if( cbk ) {
						cbk($target,null,true);
						$("#J_pop_mask").hide();
						$.mobile.hidePageLoadingMsg();
						resetPullRefresh();
						//$ul.css("visibility","visible");
					};
				}
			});
		},
		/**
		 * 显示通过ajax获取的内容。scroll to show the content pulled by ajax
		 * @param {Object} $iscroll 使用iscroll的dom的jq对象
		 * @param {Object} $items ajax获取的内容的jq对象
		 * @param {Object} opt 配置项。opt.effect 显示ajax获取的内容时是否使用渐变效果。由于移动终端性能的原因建议不使用;
		 */
		scrollShow:function($iscroll,$items,opt){
			opt=opt||{effect:false,fix:null};
			var iscr=JF.iscroll.Get($iscroll[0].id),
				y0=iscr.val.y;

			iscr.val.refresh();
			iscr.val.scrollTo(0,0);
			$("#J_pop_mask").hide();
			//opt.$ul.css("visibility","visible");
			$.mobile.hidePageLoadingMsg();

		},
		proxy_load:function(_this){
			_this=_this||JF.ajaxPool.recommend;
			var fun=function($target,cbk){
				JF.ajaxPool.recommend.load1.call(_this,$target,cbk,true);
			};
			return fun;
		},
		proxy_load1:function(_this){
			_this=_this||JF.ajaxPool.recommend;
			var fun=function($target,cbk){
				JF.ajaxPool.recommend.load1.call(_this,$target,cbk,false);
			};
			return fun;
		},
		setup:function(){
			//团购列表页数据
			var me=this;
			$("#p11").bind("pageshow",function(){
				if(!me.isInit){
					$.mobile.showPageLoadingMsg();

					me.load1($("#is_p11"),function($t,data,isError){
						$.mobile.hidePageLoadingMsg();
						me.isInit=true;
						$("#ebtn_more").html(p.opts.txt_btnMore).data("busy",false).show();

						$("#is_p11 .iScrollPullDown,#is_p11 .iScrollPullUp").css("visibility","visible");

						if(p.opts.navMGMT){
							$.mobile.navMGMT.bind("#is_p11 a",false,'vclick');
						};

					},true);
				}

			});

		}
	};

	pub.onLoad=function(){
		this.recommend.setup();
	};


	pub.init=function(opts){
		p.opts=$.extend({
			num_pp:10, //列表每页数据数
			txt_btnMore:'查看更多',
			txt_btnMore0:'查看更多',
			txt_btnLoading:'加载中...'
		},opts||{});
	};

	return pub;

})(jQuery));